#include "tim_app.h"

void Tim_set_duty(uint16_t duty)
{
    TIM2->CCR2 = (TIM2->ARR +1 )* (duty / 100.0f);
}

void tim3_set_duty(uint8_t duty)
{
	TIM3->CCR2 = (TIM3->ARR + 1) * (duty / 100.0f);
}
void Tim_set_freq(uint16_t freq)
{
    uint32_t Clock_freq = 1000000;

    uint32_t value = Clock_freq / freq - 1;

    TIM2->CCR2 = (value + 1) * (TIM2->CCR2 / TIM2->ARR);

    TIM2->ARR = value;

    TIM2->EGR = TIM_EGR_UG;
}

void Tim3_set_freq(uint16_t freq)
{
    uint32_t Clock_freq = 1000000;

    uint32_t value = Clock_freq / freq - 1;

    TIM3->CCR2 = (value + 1) * (TIM3->CCR2 / TIM3->ARR);

    TIM3->ARR = value;

    TIM3->EGR = TIM_EGR_UG;
}

uint16_t old_data[4];
void tim_proc(void)
{
    if(old_data[0]!= PA1.duty || old_data[1]!= PA1.freq || old_data[2]!= PA7.duty || old_data[3]!= PA7.freq)
    {
        old_data[0] == PA1.duty;
        old_data[1] == PA1.freq;
        old_data[2] == PA7.duty;
        old_data[3] == PA7.freq;

        Tim_set_freq(PA1.freq);
        Tim3_set_freq(PA7.freq);

        Tim_set_duty(PA1.duty);
        tim3_set_duty(PA7.duty);
    }
}
